package com.juku2024.juku

import android.content.Intent
import android.os.Bundle
import android.util.SparseArray
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.juku2024.juku.adapter.DownloadTagFragmentAdapter
import com.juku2024.juku.base.BaseActivity
import com.juku2024.juku.model.DownloadActionModel
import com.juku2024.juku.widget.HeaderView
import com.wanban.download.WBDownloadUtils
import org.greenrobot.eventbus.EventBus

class DownloadDetailActivity : BaseActivity() {

    private lateinit var header: HeaderView
    private lateinit var viewPager: ViewPager2
    private lateinit var tabLayout: TabLayout

    private val statusSparse = SparseArray<String>()
    private val downloadTagAdapter by lazy { DownloadTagFragmentAdapter(this) }

    override fun getLayoutResId(): Int {
        return R.layout.activity_download_detail
    }

    override fun initView() {
        super.initView()
        header = findViewById(R.id.header_download_detail)
        viewPager = findViewById(R.id.viewPager_download_detail)
        tabLayout = findViewById(R.id.tabLayout_download_detail)


        statusSparse.put(0, getString(R.string.edit))
        statusSparse.put(1, getString(R.string.edit))
        viewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL
        viewPager.isUserInputEnabled = true
        viewPager.adapter = downloadTagAdapter
        TabLayoutMediator(tabLayout, viewPager, true) { tab, position ->
            tab.text = downloadTagAdapter.tabNames[position]
        }.attach()
    }

    override fun init(savedInstanceState: Bundle?) {
        WBDownloadUtils.resumeAllDownloads()
    }

    override fun bindEvent() {
        super.bindEvent()
        header.setOnEditClickListener{ // 点击编辑
            if (viewPager.currentItem >= 1) {
                return@setOnEditClickListener
            }
            val currentStatus = statusSparse[viewPager.currentItem]
            val isShowEditStatus = currentStatus == getString(R.string.edit)
            // 要根据当前所属tab来设置
            EventBus.getDefault().post(DownloadActionModel(viewPager.currentItem, isShowEditStatus))
        }
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                val currentItem = viewPager.currentItem
                val currentStatus = statusSparse[currentItem]
                if (tab?.text == "正在下载") {
                    header.setMenuText(currentStatus)
                } else {
                    header.setMenuText(null)
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {}

            override fun onTabReselected(tab: TabLayout.Tab?) {}
        })
    }


    @Suppress("DEPRECATION")
    @Deprecated("Deprecated in Java")
    override fun onBackPressed() {
        // 获取当前页面的编辑状态
        val currentStatus = statusSparse[viewPager.currentItem]
        val isShowEditStatus = currentStatus == getString(R.string.complete)
        if (isShowEditStatus) { // 要还原显示状态
            EventBus.getDefault().post(DownloadActionModel(viewPager.currentItem, false))
            return
        }

        if (isNeedToMain()) {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        } else {
            super.onBackPressed()
        }
    }


    fun resetEditStatus(){
        statusSparse.put(0, getString(R.string.edit))
        statusSparse.put(1, getString(R.string.edit))
        header.setMenuText(statusSparse[0])
    }

    fun changeEditStatus(){
        val currentItem = viewPager.currentItem
        val currentStatus = statusSparse[currentItem]
        val isShowEditStatus = currentStatus == getString(R.string.edit)
        val newStr = if (isShowEditStatus) {
            getString(R.string.complete)
        } else {
            getString(R.string.edit)
        }
        header.setMenuText(newStr)
        statusSparse.put(currentItem, newStr)
    }
}